package com.ego.sender.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

@Configuration
public class SenderConfig {

    @Value("${ego.rabbitmq.content.queueName}")
    private String contentQueue;
    @Value("${ego.rabbitmq.item.insertName}")
    private String itemInsert;
    @Value("${ego.rabbitmq.item.deleteName}")
    private String itemDelete;
    @Value("${ego.rabbitmq.order.createorder}")
    private String createOrder;
    @Value("${ego.rabbitmq.order.deletecart}")
    private String deleteCart;
    @Value("${ego.rabbitmq.mail}")
    private String mail;

    /**
     * 如果没有队列，帮助创建队列。
     *
     * @return
     */
    @Bean
    public Queue queue() {
        return new Queue(contentQueue);
    }


    @Bean
    public DirectExchange directExchange() {
        return new DirectExchange("amq.direct");
    }

    @Bean
    // 参数名和方法名一直就是从spring容器中获取对应方法的返回值
    public Binding binding(Queue queue, DirectExchange directExchange) {
        return BindingBuilder.bind(queue).to(directExchange).withQueueName();
    }

    /*
    写下面两个方法的目的：
    第一次发送消息时，如果没有队列创建队列，并绑定到amq.direct交换器上
     */
    @Bean
    public Queue queueItemInsert() {
        return new Queue(itemInsert);
    }

    @Bean
    public Binding binding2(Queue queueItemInsert, DirectExchange directExchange) {
        return BindingBuilder.bind(queueItemInsert).to(directExchange).withQueueName();
    }


    @Bean
    public Queue queueItemDelete() {
        return new Queue(itemDelete);
    }

    @Bean
    public Binding binding3(Queue queueItemDelete, DirectExchange directExchange) {
        return BindingBuilder.bind(queueItemDelete).to(directExchange).withQueueName();
    }

    /*
    订单队列
     */
    @Bean
    public Queue queueCreateOrder() {
        return new Queue(createOrder);
    }

    @Bean
    public Binding binding4(Queue queueCreateOrder, DirectExchange directExchange) {
        return BindingBuilder.bind(queueCreateOrder).to(directExchange).withQueueName();
    }

    /*
    删除购物车商品队列
     */
    @Bean
    public Queue queueDeleteCart() {
        return new Queue(deleteCart);
    }

    @Bean
    public Binding binding5(Queue queueDeleteCart, DirectExchange directExchange) {
        return BindingBuilder.bind(queueDeleteCart).to(directExchange).withQueueName();
    }

    @Bean
    public Queue queueMail() {
        return new Queue(mail);
    }

    @Bean
    public Binding binding6(Queue queueMail, DirectExchange directExchange) {
        return BindingBuilder.bind(queueMail).to(directExchange).withQueueName();
    }
}
